package com.greyu.ysj.utils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.greyu.ysj.config.Constants;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class WalletProcess {
	
	/**
	 * 创建钱包地址
	 * @return
	 */
	public WalletAccount GetWalletAccount() {

		String path = Constants.COIN_SERVER_PATH + "createAccount";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		RequestBody body = new FormBody.Builder().build();
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();
		System.out.println("request++++"+request);
		System.out.println(request.header("tag"));//null
		System.out.println(request.method());
		System.out.println(request.url());
		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string+"^^^^^^^^");
				JSONObject tmpobject = JSON.parseObject(string);
				String result = tmpobject.getString("result");
				JSONObject jsonObject = JSON.parseObject(result); 
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					walletAccount.setWalletBtcAddress(jsonObject.getString("BTCaddress"));
					walletAccount.setWalletBtcPrivatekey(jsonObject.getString("BTCprivateKey"));
					walletAccount.setWalletEthAddress(jsonObject.getString("AKBaddress"));
					walletAccount.setWalletEthPrivatekey(jsonObject.getString("AKBprivateKey"));
					walletAccount.setWalletEosAddress(jsonObject.getString("EOSaddress"));
					walletAccount.setResult(true);
					return walletAccount;
				}
				System.out.println("return  createAccount error");
				walletAccount.setResult(false);
				return walletAccount;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}
	
	// 获取CBT价格
	public WalletAccount getCbtPrice() {
		WalletAccount walletAccount = new WalletAccount();
		walletAccount.setCbtPrice(1.0f);
		return walletAccount;
	}
	
	/**
	 * 检查提现交易状态
	 */
	//获取hash状态 0成功，1失败，2正在进行
	public WalletAccount DoCheckTx(String hash) {
		String path = Constants.COIN_SERVER_PATH + "checkTx";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("hash", hash);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					int resultRet = tmpobject.getIntValue("result");
					walletAccount.setHashStatus(resultRet);
					walletAccount.setResult(true);
					return walletAccount;
				}
				//
				System.out.println("return  checkTx error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}

	/**
	 * 用户 提现
	 * @param toaddress
	 * @param value
	 * @return
	 */
	public WalletAccount DoRedemptionETH(String toaddress, double value) {
		String path = Constants.COIN_SERVER_PATH + "withDrawAKB";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("address", toaddress);
		jsonObject.put("value", value);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					String resultHash = tmpobject.getString("result");
					walletAccount.setResult(true);
					walletAccount.setHash(resultHash);
					return walletAccount;
				}
				//
				System.out.println("return withDrawAKB error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}
	
	
	
	public WalletAccount GetDFCWalletAccount() {

		String path = Constants.COIN_SERVER_PATH1 + "createAccount";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		RequestBody body = new FormBody.Builder().build();
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();
		System.out.println("request++++"+request);
		System.out.println(request.header("tag"));//null
		System.out.println(request.method());
		System.out.println(request.url());
		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string+"^^^^^^^^");
				JSONObject tmpobject = JSON.parseObject(string);
				String result = tmpobject.getString("result");
				JSONObject jsonObject = JSON.parseObject(result); 
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					walletAccount.setWalletBtcAddress(jsonObject.getString("ETHaddress"));
					walletAccount.setWalletBtcPrivatekey(jsonObject.getString("ETHprivateKey"));
					walletAccount.setWalletEthAddress(jsonObject.getString("AKBaddress"));
					walletAccount.setWalletEthPrivatekey(jsonObject.getString("AKBprivateKey"));
					walletAccount.setWalletEosAddress(jsonObject.getString("EOSaddress"));
					walletAccount.setResult(true);
					return walletAccount;
				}
				System.out.println("return  createAccount error");
				walletAccount.setResult(false);
				return walletAccount;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}
	
	
	
	/**
	 * 检查提现交易状态
	 */
	//获取hash状态 0成功，1失败，2正在进行
	public WalletAccount DoDFCCheckTx(String hash) {
		String path = Constants.COIN_SERVER_PATH1 + "checkTx";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("hash", hash);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					int resultRet = tmpobject.getIntValue("result");
					walletAccount.setHashStatus(resultRet);
					walletAccount.setResult(true);
					return walletAccount;
				}
				//
				System.out.println("return  checkTx error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}

	/**
	 * 用户 提现
	 * @param toaddress
	 * @param value
	 * @return
	 */
	public WalletAccount DoRedemptionDFC(String toaddress, double value) {
		String path = Constants.COIN_SERVER_PATH1 + "withdrawDFC";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("address", toaddress);
		jsonObject.put("value", value);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					String resultHash = tmpobject.getString("result");
					walletAccount.setResult(true);
					walletAccount.setHash(resultHash);
					return walletAccount;
				}
				//
				System.out.println("return withDrawAKB error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}
	
	public WalletAccount GetWalletUSDTAccount() {

		String path = Constants.COIN_SERVER_PATH2 + "createAccount";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		RequestBody body = new FormBody.Builder().build();
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();
		System.out.println("request++++"+request);
		System.out.println(request.header("tag"));//null
		System.out.println(request.method());
		System.out.println(request.url());
		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string+"^^^^^^^^");
				JSONObject tmpobject = JSON.parseObject(string);
				String result = tmpobject.getString("result");
				JSONObject jsonObject = JSON.parseObject(result);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					walletAccount.setWalletBtcAddress(jsonObject.getString("ETHaddress"));
					walletAccount.setWalletBtcPrivatekey(jsonObject.getString("ETHprivateKey"));
					walletAccount.setWalletEthAddress(jsonObject.getString("AKBaddress"));
					walletAccount.setWalletEthPrivatekey(jsonObject.getString("AKBprivateKey"));
					walletAccount.setWalletEosAddress(jsonObject.getString("EOSaddress"));
					walletAccount.setResult(true);
					return walletAccount;
				}
				System.out.println("return  createAccount error");
				walletAccount.setResult(false);
				return walletAccount;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}

	// 获取CBT价格
	public WalletAccount getUSDTPrice() {
		WalletAccount walletAccount = new WalletAccount();
		walletAccount.setCbtPrice(1.0f);
		return walletAccount;
	}

	/**
	 * 检查提现交易状态
	 */
	//获取hash状态 0成功，1失败，2正在进行
	public WalletAccount DoUSDTCheckTx(String hash) {
		String path = Constants.COIN_SERVER_PATH2 + "checkTx";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("hash", hash);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					int resultRet = tmpobject.getIntValue("result");
					walletAccount.setHashStatus(resultRet);
					walletAccount.setResult(true);
					return walletAccount;
				}
				//
				System.out.println("return  checkTx error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}

	/**
	 * 用户 提现
	 * @param toaddress
	 * @param value
	 * @return
	 */
	public WalletAccount DoUSDTRedemptionETH(String toaddress, double value) {
		String path = Constants.COIN_SERVER_PATH2 + "withdrawUSDT";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("address", toaddress);
		jsonObject.put("value", value);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					String resultHash = tmpobject.getString("result");
					walletAccount.setResult(true);
					walletAccount.setHash(resultHash);
					return walletAccount;
				}
				//
				System.out.println("return withdrawUSDT error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}
	
	//AKB分币
	public WalletAccount DoAKBdistribute(double amount) {
		String path = Constants.COIN_SERVER_PATH + "distribute";
		WalletAccount walletAccount = new WalletAccount();
		// 2 创建okhttpclient对象
		// OkHttpClient client = new OkHttpClient();
		OkHttpClient.Builder client = new OkHttpClient.Builder();
		client.readTimeout(50000, TimeUnit.MILLISECONDS);
		client.writeTimeout(50000, TimeUnit.MILLISECONDS);
		client.connectTimeout(50000, TimeUnit.MILLISECONDS);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("amount", amount);

		// RequestBody body = new FormBody.Builder().add("address", address).build();
		RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
				jsonObject.toJSONString());
		System.out.println(jsonObject.toJSONString());
		// 3 创建请求方式
		Request request = new Request.Builder().url(path).post(body).build();

		// 4 执行请求操作
		try {
			Response response = client.build().newCall(request).execute();
			if (response.isSuccessful()) {
				String string = response.body().string();
				System.out.println(string);
				JSONObject tmpobject = JSON.parseObject(string);
				String code = tmpobject.getString("code");
				if (code.equals("200")) {
					walletAccount.setResult(true);
					return walletAccount;
				}
				//
				System.out.println("return  checkTx error");
				walletAccount.setResult(false);
				return walletAccount;
				// return -1;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		walletAccount.setResult(false);
		return walletAccount;
	}
	
	//USDT分币
			public WalletAccount DoUSDTdistribute(double amount) {
				String path = Constants.COIN_SERVER_PATH2 + "distribute";
				WalletAccount walletAccount = new WalletAccount();
				// 2 创建okhttpclient对象
				// OkHttpClient client = new OkHttpClient();
				OkHttpClient.Builder client = new OkHttpClient.Builder();
				client.readTimeout(50000, TimeUnit.MILLISECONDS);
				client.writeTimeout(50000, TimeUnit.MILLISECONDS);
				client.connectTimeout(50000, TimeUnit.MILLISECONDS);

				JSONObject jsonObject = new JSONObject();
				jsonObject.put("amount", amount);

				// RequestBody body = new FormBody.Builder().add("address", address).build();
				RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"),
						jsonObject.toJSONString());
				System.out.println(jsonObject.toJSONString());
				// 3 创建请求方式
				Request request = new Request.Builder().url(path).post(body).build();

				// 4 执行请求操作
				try {
					Response response = client.build().newCall(request).execute();
					if (response.isSuccessful()) {
						String string = response.body().string();
						System.out.println(string);
						JSONObject tmpobject = JSON.parseObject(string);
						String code = tmpobject.getString("code");
						if (code.equals("200")) {
							walletAccount.setResult(true);
							return walletAccount;
						}
						//
						System.out.println("return  checkTx error");
						walletAccount.setResult(false);
						return walletAccount;
						// return -1;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				walletAccount.setResult(false);
				return walletAccount;
			}
			
			public Object WalletInfo() {

				String path = Constants.COIN_SERVER_PATH + "getPoolDetail";
				WalletAccount walletAccount = new WalletAccount();
				// 2 创建okhttpclient对象
				OkHttpClient.Builder client = new OkHttpClient.Builder();
				client.readTimeout(50000, TimeUnit.MILLISECONDS);
				client.writeTimeout(50000, TimeUnit.MILLISECONDS);
				client.connectTimeout(50000, TimeUnit.MILLISECONDS);

				RequestBody body = new FormBody.Builder().build();
				// 3 创建请求方式
				Request request = new Request.Builder().url(path).post(body).build();
				System.out.println("request++++"+request);
				System.out.println(request.header("tag"));//null
				System.out.println(request.method());
				System.out.println(request.url());
				// 4 执行请求操作
				try {
					Response response = client.build().newCall(request).execute();
					if (response.isSuccessful()) {
						String string = response.body().string();
						System.out.println(string+"^^^^^^^^");
						JSONObject tmpobject = JSON.parseObject(string);
						String result = tmpobject.getString("poolDetail");
						
						JSONArray parseArray = JSONArray.parseArray(result);
						
						return parseArray;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				return null;
			}
	
	
}
